#!/usr/bin/env python3
#-*- coding:utf8 -*-
# Power by 2020-06-13 00:48:57

import os
import random
import paddle
import paddle.fluid as fluid
from paddle.fluid.dygraph.nn import Conv2D,Pool2D,Linear
import numpy as np
from PIL import Image
import gzip
import json

import sys
curpath=os.path.abspath(os.curdir)
sys.path.append(curpath)
import model

def load_image(img_path):
    """TODO: Docstring for load_image.

    :img_path: TODO
    :returns: TODO

    """
    im=Image.open(img_path).convert('L')
    im.show()
    im=im.resize((28,28),Image.ANTIALIAS)
    im=np.array(im).reshape(1,1,28,28).astype(np.float32)
    im=1.0 - im/255.
    return im
with fluid.dygraph.guard():
    model=model.MNIST()
    img_path='./example_6.jpg'
    model_dict,_=fluid.load_dygraph('./mnist-model.pdparams')
    model.load_dict(model_dict)
    model.eval()
    tensor_img=load_image(img_path)
    results=model(fluid.dygraph.to_variable(tensor_img))
    lab=np.argsort(results.numpy())
    print("the num is:{}".format(lab[0][-1]))
